﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using OfficialWeb.Application;

using OOS.Application.Category.Dtos;
using OOS.Application.Category.Services.Interfaces;
using OOS.Application.User.Services.Interfaces;
using OOS.Core.Category;
using OOS.Core.RelyOn;

namespace OOS.Application.Category.Services
{
    /// <summary>
    /// 分类管理
    /// </summary>
    public class CategoryBasicService : ICategoryBasicService, ITransient
    {
        private readonly ISqlSugarClient _client;
        private readonly IUserBasicService _userBasicService;

        public CategoryBasicService(ISqlSugarClient client, IUserBasicService userBasicService)
        {
            _client = client;
            _userBasicService = userBasicService;
        }

        /// <summary>
        /// 根据Id删除分类信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteCategoryBasicById(int id)
        {
            CategoryBasic categoryBasic = SelectCategoryBasicById(id);
            categoryBasic.IsDeleted = true;
            return UpdateCategoryBasicById(categoryBasic);
        }

        /// <summary>
        /// 新增分类
        /// </summary>
        /// <param name="categoryBasic"></param>
        /// <returns></returns>
        public bool InsertCategoryBasic(CategoryBasic categoryBasic)
        {
            if (categoryBasic == null) { return false; }
            //判断token中是否有用户数据
            int userId = JWTToken.GetUser().Id;
            if (userId <= 0) { return false; }
            categoryBasic.CreateUser = userId;
            categoryBasic.CreateDate = DateTime.Now;
            categoryBasic.UpdateUser = userId;
            categoryBasic.UpdateDate = DateTime.Now;
            categoryBasic.IsDeleted = false;
            return _client.Insertable(categoryBasic).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 根据id查询分类详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public CategoryBasicDto SelectCategoryById(int id)
        {
            if(id <= 0) return new CategoryBasicDto();
            CategoryBasicDto categoryBasicDto = SelectCategoryBasicById(id).Adapt<CategoryBasicDto>();
            if(categoryBasicDto == null) return new CategoryBasicDto();
            int CreateId = int.Parse(categoryBasicDto.CreateUser);
            int UpdateId = int.Parse(categoryBasicDto.UpdateUser);
            categoryBasicDto.CreateUser = _userBasicService.SelectUserById(CreateId).Username;
            categoryBasicDto.UpdateUser = _userBasicService.SelectUserById(UpdateId).Username;
            return categoryBasicDto;
        }

        /// <summary>
        /// 按条件(分类名称模糊查询，分类类型精确查询)分页查询
        /// </summary>
        /// <param name="categoryBasic"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public PageResponseDto<List<CategoryBasicDto>> SelectPageByConditions(CategoryBasic categoryBasic, int pageIndex, int pageSize)
        {
            int size = 0;
            List<CategoryBasicDto> categoryBasicDtos = _client.Queryable<CategoryBasic>()
                .Where(c => c.IsDeleted == false)
                .WhereIF(!String.IsNullOrEmpty(categoryBasic.CategoryName),c => c.CategoryName.Contains(categoryBasic.CategoryName))
                .WhereIF(categoryBasic.Type != 0, c => c.Type == categoryBasic.Type)
                .OrderBy(c =>c.Sort,OrderByType.Desc)
                .OrderBy(ub => ub.CreateDate, OrderByType.Desc)
                .ToPageList(pageIndex,pageSize,ref size)
                .Adapt<List<CategoryBasicDto>>();
            
            for (int i = 0; i<categoryBasicDtos.Count;i++)
            {
                categoryBasicDtos[i] = SelectCategoryById(categoryBasicDtos[i].Id);
            }
            PageResponseDto<List<CategoryBasicDto>> pageResponseDto = new PageResponseDto<List<CategoryBasicDto>>();
            pageResponseDto.data = categoryBasicDtos;
            pageResponseDto.size = size;
            return pageResponseDto;
        }

        /// <summary>
        /// 根据Id修改分类信息
        /// </summary>
        /// <param name="categoryBasic"></param>
        /// <returns></returns>
        public bool UpdateCategoryBasicById(CategoryBasic categoryBasic)
        {
            if (categoryBasic == null) return false;
            CategoryBasic category = SelectCategoryBasicById(categoryBasic.Id);
            if (category == null) return false;
            //判断token中是否有用户数据
            int userId = JWTToken.GetUser().Id;
            if (userId <= 0) { return false; }
            category.UpdateUser = userId;
            category.UpdateDate = DateTime.Now;
            category.CategoryName = categoryBasic.CategoryName;
            category.Sort = categoryBasic.Sort;
            category.Description = categoryBasic.Description;
            category.IsDeleted = categoryBasic.IsDeleted;
            return _client.Updateable(category).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 根据id查询数据详情(数据库原始数据)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private CategoryBasic SelectCategoryBasicById(int id)
        {
            return _client.Queryable<CategoryBasic>()
                .Where(c => c.Id == id)
                .Where(c => c.IsDeleted == false)
                .Single();
        }
    }
}
